﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using HuaCheng.DataAccess;
using HuaCheng.Model;
using HuaCheng.CommonClass;

namespace HuaCheng.Views
{
    public partial class MachineInfo : Form
    {
        static List<string> changedIPs = new List<string>();
        public AutoConnect auto;
        public MachineInfo()
        {
            auto = new AutoConnect(changedIPs);
            InitializeComponent();
            loadTable();
        }
        /// <summary>
        /// 加载表格
        /// </summary>
        private void loadTable()
        {
            if (DeviceHelper.queryMachineInfo().Count!=0)
            {
                List<Device> device = DeviceHelper.queryMachineInfo();
                //bindData(device);
                ////设置显示的列名
                //dgvMachineInfo.Columns["Cabinet"].HeaderText = "柜号";
                //dgvMachineInfo.Columns["DevId"].HeaderText = "中位机ID";
                //dgvMachineInfo.Columns["DevMac"].HeaderText = "中位机Mac";
                //dgvMachineInfo.Columns["DevIP"].HeaderText = "中位机IP";
                //dgvMachineInfo.Columns["DevType"].HeaderText = "中位机类型";
                //dgvMachineInfo.Columns["DevName"].Visible=false;
                //dgvMachineInfo.Columns["DevDelete"].Visible = false;
                for (int i = 0; i < device.Count; i++)
                {
                    dgvMachineInfo.Rows.Add();
                    dgvMachineInfo.Rows[i].Cells[0].Value = device[i].GID;
                    dgvMachineInfo.Rows[i].Cells[1].Value = device[i].DevId;
                    dgvMachineInfo.Rows[i].Cells[2].Value = device[i].DevMac;
                    dgvMachineInfo.Rows[i].Cells[3].Value = device[i].MID.ToString();
                    dgvMachineInfo.Rows[i].Cells[4].Value = device[i].DevType;
                    dgvMachineInfo.Rows[i].Cells[5].Value = device[i].DevIP;
                }
            }
            else
            {
                //dgvMachineInfo.Rows.Add(1);
                ////初始化表格
                //DataGridViewColumn dc1 = new DataGridViewColumn();
                //dc1.HeaderText="柜号";
                //dgvMachineInfo.Columns.Add(dc1);
                //DataGridViewColumn dc2 = new DataGridViewColumn();
                //dc2.HeaderText = "中位机ID";
                //dgvMachineInfo.Columns.Add(dc2);
                //DataGridViewColumn dc3 = new DataGridViewColumn();
                //dc3.HeaderText = "中位机Mac";
                //dgvMachineInfo.Columns.Add(dc3);
                //DataGridViewColumn dc4 = new DataGridViewColumn();
                //dc4.HeaderText = "中位机IP";
                //dgvMachineInfo.Columns.Add(dc4);
                //DataGridViewColumn dc5 = new DataGridViewColumn();
                //dc5.HeaderText = "中位机类型";
                //dgvMachineInfo.Columns.Add(dc5);
            }
        }
        /// <summary>
        /// 绑定数据源
        /// </summary>
        /// <param name="device"></param>
        //public void bindData(List<Device> device)
        //{
        //    //将List转化为DataTable
        //    DataTable dt = DataTableHelper.ToDataTable<Device>(device);
        //    //初始化BindingSource
        //    BindingSource bs = new BindingSource();
        //    //绑定DataTable
        //    bs.DataSource = dt;
        //    //将dgvMachineInfo的数据源绑定成BingdingList
        //    dgvMachineInfo.DataSource = bs;
        //}
        /// <summary>
        /// 确定点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvMachineInfo.Rows.Count-1; i++)
            {
                string cabinet = "";
                string devid = "";
                string devmac = "";
                string mid = "";
                string devtype = "";
                string devip = "";
                if (dgvMachineInfo.Rows[i].Cells[0].Value != null)
                {
                    cabinet = dgvMachineInfo.Rows[i].Cells[0].Value.ToString();
                }
                if (dgvMachineInfo.Rows[i].Cells[1].Value != null)
                {
                    devid = dgvMachineInfo.Rows[i].Cells[1].Value.ToString();
                }
                if (dgvMachineInfo.Rows[i].Cells[2].Value != null)
                {
                    devmac = dgvMachineInfo.Rows[i].Cells[2].Value.ToString();
                }
                if (dgvMachineInfo.Rows[i].Cells[3].Value != null)
                {
                    mid = dgvMachineInfo.Rows[i].Cells[3].Value.ToString();
                }
                if (dgvMachineInfo.Rows[i].Cells[4].Value != null)
                {
                    devtype = dgvMachineInfo.Rows[i].Cells[4].Value.ToString();
                }
                if (dgvMachineInfo.Rows[i].Cells[5].Value != null)
                {
                    devip = dgvMachineInfo.Rows[i].Cells[5].Value.ToString();
                }
                if (cabinet == "")
                {
                    MessageBox.Show("第" + (i + 1) + "行的柜号未设置！");
                    break;
                }
                else if (devid == "")
                {
                    MessageBox.Show("第" + (i + 1) + "行的中位机ID未设置！");
                    break;
                }
                else if (devmac == "")
                {
                    MessageBox.Show("第" + (i + 1) + "行的中位机Mac未设置！");
                    break;
                }
                else if (mid == "")
                {
                    MessageBox.Show("第" + (i + 1) + "行的中位机号未设置！");
                    break;
                }
                else if (devtype == "")
                {
                    MessageBox.Show("第" + (i + 1) + "行的中位机类型未设置！");
                    break;
                }
                else
                {
                    Device device = new Device()
                    {
                        GID = Convert.ToInt32(cabinet),
                        DevId = devid,
                        DevMac = devmac,
                        MID=Convert.ToInt32(mid),
                        DevType = devtype,
                        DevIP = devip
                    };
                    DeviceHelper.AddDevInfo(device);
                    changedIPs.Add(devip);
                    if (i == dgvMachineInfo.Rows.Count - 2)
                    {
                        
                        if (DialogResult.Yes == MessageBox.Show("执行自动组网会使所有设备通道恢复为初始完成状态，确定执行？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                        {
                            //auto = new AutoConnect(allDev,changedIPs);
                            auto.StartPosition = FormStartPosition.CenterScreen;
                            auto.ShowDialog();
                        }
                        this.Close();
                    }
                }
            }

        }
        /// <summary>
        /// 给第一列赋值序号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvMachineInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dgvMachineInfo.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dgvMachineInfo.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //选中的行数
                int iCount = dgvMachineInfo.SelectedRows.Count;
                if (iCount < 1)
                {
                    MessageBox.Show("请选择要删除的行！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (DialogResult.Yes == MessageBox.Show("是否删除选中的数据？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    foreach (DataGridViewRow dr in dgvMachineInfo.SelectedRows)
                    {
                        if (dr.IsNewRow == false)
                        {
                            if (dr.Cells[2].Value != null)
                            {
                                string devMac = dr.Cells[2].Value.ToString();
                                DeviceHelper.deleteDev(devMac);
                                //dgvMachineInfo.Rows.Remove(dr);
                            }
                            dgvMachineInfo.Rows.Remove(dr);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + ex.StackTrace);
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 全选按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAllSelect_Click(object sender, EventArgs e)
        {
            int selectedRows = dgvMachineInfo.SelectedRows.Count;
            if (selectedRows == dgvMachineInfo.Rows.Count)
            {
                foreach (DataGridViewRow dr in dgvMachineInfo.SelectedRows)
                {
                    dr.Selected = false;
                }
            }
            else
            {
                dgvMachineInfo.SelectAll();
            }
        }
        /// <summary>
        /// 添加按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            dgvMachineInfo.Rows.Add(1);
        }
        #region 国际化
        
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("MachineInfo");
            this.cabinet.HeaderText = ResourceCulture.GetString("Group");
            this.devId.HeaderText = ResourceCulture.GetString("Machine") + " ID";
            this.devMac.HeaderText = ResourceCulture.GetString("Machine") + " Mac";
            this.devNum.HeaderText = ResourceCulture.GetString("MachineNum");
            this.devType.HeaderText = ResourceCulture.GetString("MachineType");
            this.devIp.HeaderText = ResourceCulture.GetString("Machine") + " IP";
            this.btnAllSelect.Text = ResourceCulture.GetString("SelectAll");
            this.btnAdd.Text = ResourceCulture.GetString("Add");
            this.btnDelete.Text = ResourceCulture.GetString("Delete");
            this.btnOk.Text = ResourceCulture.GetString("LoginOk");
            this.btnCancel.Text = ResourceCulture.GetString("LoginCancel");
        }
        #endregion

        private void MachineInfo_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
    }
}
